/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logic;

import entityBeans.Interaction;
import entityBeans.Specimen;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import logic.mapping.XLSToEntity;
import xlsBeans.ImportResponse;
import xlsBeans.LineXLS;

/**
 *
 * @author allankv
 */
public class Import {

    public Import() {
    }


    // FALTA O DELETE!
    public ImportResponse startImporating(String url) throws SQLException, ClassNotFoundException {
        ImportResponse rs = new ImportResponse();
        SpreadsheetHandling s = new SpreadsheetHandling(url);

        List<LineXLS> lList = s.readSpreadsheet();

        List<Specimen> listSpecimen = new ArrayList<Specimen>();
        List<Specimen> listDeleteSpecimen = new ArrayList<Specimen>();
        List<Interaction> listInteraction = new ArrayList<Interaction>();
        XLSToEntity m = new XLSToEntity();
        DatabaseHandling d = new DatabaseHandling();
        Specimen sp1 = new Specimen();
        Specimen sp2 = new Specimen();
        boolean add;

        //mapeia specimes e interacoes
        for (LineXLS l : lList) {
            if (l.getSp1().isSet()) {
                sp1 = m.mapping(l.getSp1());
                if (sp1 != null) {
                    sp1.getRecordlevelelement().setGlobaluniqueidentifier(sp1.getRecordlevelelement().getInstitutioncode().getInstitutioncode()
                            + ":" + sp1.getRecordlevelelement().getCollectioncode().getCollectioncode()
                            + ":" + sp1.getOccurrenceelement().getCatalognumber());
                    if (l.getSp1().getDelete()) { //DELETE
                        add = true;
                        for (Specimen aux : listDeleteSpecimen) {
                            if (aux.getRecordlevelelement().getGlobaluniqueidentifier().equals(sp1.getRecordlevelelement().getGlobaluniqueidentifier())) {
                                add = false;
                            }
                        }
                        if (add == true) {
                            listDeleteSpecimen.add(sp1);
                        }
                    } else { // CREATE or UPDATE
                        add = true;
                        for (Specimen aux : listSpecimen) {
                            if (aux.getRecordlevelelement().getGlobaluniqueidentifier().equals(sp1.getRecordlevelelement().getGlobaluniqueidentifier())) {
                                add = false;
                            }
                        }
                        if (add == true) {
                            listSpecimen.add(sp1);
                            rs.setTotalSpecimenCreated(rs.getTotalSpecimenCreated() + 1);
                        }
                    }
                }
            }
            if (l.getSp2().isSet()) {
                sp2 = m.mapping(l.getSp2());
                if (sp2 != null) {
                    sp2.getRecordlevelelement().setGlobaluniqueidentifier(sp2.getRecordlevelelement().getInstitutioncode().getInstitutioncode()
                            + ":" + sp2.getRecordlevelelement().getCollectioncode().getCollectioncode()
                            + ":" + sp2.getOccurrenceelement().getCatalognumber());
                    if (l.getSp2().getDelete()) { //DELETE
                        add = true;
                        for (Specimen aux : listDeleteSpecimen) {
                            if (aux.getRecordlevelelement().getGlobaluniqueidentifier().equals(sp2.getRecordlevelelement().getGlobaluniqueidentifier())) {
                                add = false;
                            }
                        }
                        if (add == true) {
                            listDeleteSpecimen.add(sp2);
                        }
                    } else { // CREATE or UPDATE
                        add = true;
                        for (Specimen aux : listSpecimen) {
                            if (aux.getRecordlevelelement().getGlobaluniqueidentifier().equals(sp2.getRecordlevelelement().getGlobaluniqueidentifier())) {
                                add = false;
                            }
                        }
                        if (add == true) {
                            listSpecimen.add(sp2);
                            rs.setTotalSpecimenCreated(rs.getTotalSpecimenCreated() + 1);
                        }
                    }
                }
            }
            if (l.getInte().isSet() && !l.getSp1().getDelete() && !l.getSp2().getDelete()) {
                Interaction i = m.mapping(l.getInte());
                i.setSpecimen1(sp1);
                i.setSpecimen(sp2);
                listInteraction.add(i);
                rs.setTotalInteractionCreated(rs.getTotalInteractionCreated()+1);
            }
        }
        //gera cache, salva objetos
        d.generateCache(listSpecimen);
        d.save(listSpecimen);//salva novo ou salva mudandacas(se gui ja existe)
        d.delete(listDeleteSpecimen);
        d.saveInteraction(listInteraction);//salva ou altera interacoes

        return rs;
    }
}

